package com.wind.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wind.activity.MainActivity;
import com.wind.base.BaseFragment;
import com.wind.beans.LogoutBean;
import com.wind.soblogin.HttpManager;
import com.debug.soblogin.R;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MeFragmenter extends BaseFragment {

    /**
     * 首先看是否存在，不存在，值都设置默认的，登陆了就设置成登陆的
     */

    private TextView mToLogin;
    private ImageView mIvAvatar;
    private TextView mTVSign;
    private LinearLayout skipToLogin;
    private int whichLayout = 0;
    private TextView mLogout;


    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {

        //TODO:这里的实时修改头像功能还不能用，不知道哪里出了问题
//
        SharedPreferences sp = Objects.requireNonNull(this.getActivity()).getSharedPreferences("isLogin", Context.MODE_PRIVATE);
        String loginValue = sp.getString("isLogin", null);
        System.out.println("ujhuhjp------>"+loginValue);




        if (loginValue==null) {
            whichLayout = 0;
            return inflater.inflate(R.layout.fragment_me, container, false);
        } else {
            whichLayout = 1;
            return inflater.inflate(R.layout.fragment_me_login, container, false);
        }

    }


    @Override
    protected void initView(View rootView) {
        //找到头像，昵称，签名
        if (whichLayout == 1) {
            mIvAvatar = rootView.findViewById(R.id.img_avatar);
            mToLogin = rootView.findViewById(R.id.to_login);
            mTVSign = rootView.findViewById(R.id.tv_sign);
            SharedPreferences sp = Objects.requireNonNull(this.getActivity()).getSharedPreferences("userInfo", Context.MODE_PRIVATE);
            String avatar = sp.getString("avatar", null);
            String name = sp.getString("name", null);
            String sign = sp.getString("sign", null);

            mToLogin.setText(name);
            mTVSign.setText(sign);
            Glide.with(Objects.requireNonNull(getContext())).load(avatar).into(mIvAvatar);


            mLogout = rootView.findViewById(R.id.log_out);
            mLogout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    HttpManager
                            .getSobApi()
                            .Logout()
                            .enqueue(new Callback<LogoutBean>() {
                                @Override
                                public void onResponse(Call<LogoutBean> call, Response<LogoutBean> response) {
                                    LogoutBean token = response.body();
                                    if (token.getCode()==20000) {
                                        Toast.makeText(getContext(), token.getMessage(), Toast.LENGTH_LONG).show();
                                        SharedPreferences sp = getActivity().getSharedPreferences("isLogin", Context.MODE_PRIVATE);
                                        SharedPreferences.Editor editor = sp.edit();
                                        String loginValue = "1";
                                        editor.putString("isLogin", loginValue);
                                        editor.commit();
                                        //从token里获取信息
                                        Intent intent = new Intent(getContext(), MainActivity.class);
                                        startActivity(intent);
                                    }else {
                                        Toast.makeText(getContext(), token.getMessage(), Toast.LENGTH_LONG).show();
                                    }




                                }

                                @Override
                                public void onFailure(Call<LogoutBean> call, Throwable t) {

                                }
                            });
                }
            });
        } else {
            skipToLogin = rootView.findViewById(R.id.skip_to_login);


            skipToLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getContext(), MainActivity.class);
                    startActivity(intent);
                }
            });

        }


    }


}
